'''
爬取网址(中国五矿集团有限公司)：
https://ec.minmetals.com.cn/open/home/purchase-info

这是一个webpack项目的网址案例。
难点：
    https://ec.minmetals.com.cn/open/homepage/public 这个请求获取公钥
    https://ec.minmetals.com.cn/open/homepage/zbs/by-lx-page 数据源请求，js通过public请求返回公钥对参数加密，并请求；
    加密算法用的RSA 签名sign是md5

    sign: B()(JSON.stringify(e)),
        e：是请求参数；
        B()(JSON.stringify(e))：是md5加密后的返回值；
        验证是不是md5，打开在线验证md5网址(https://www.sojson.com/encrypt_md5.html#google_vignette)，加密后，做对比；
        加密工具：用到了 crypto-js 加密js

    RSA算法是这种的
    控制台输出：JSON.stringify(new d["a"])
        {"default_key_size":1024,"default_public_exponent":"010001","log":false,"key":null}


'''



import requests

cookies = {
    'Hm_lvt_f32803886966beff8fa513f7a33dea1e': '1726442768',
    '__jsluid_s': 'd88cae93286614d37d244f1579777df3',
    'SUNWAY-ESCM-COOKIE': '88a7f2ec-63a6-483c-b19b-c79efb470f31',
    'JSESSIONID': '5C3AFC181C8A6D0DCC0547402F44140F',
}

headers = {
    'Accept': 'application/json, text/plain, */*',
    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
    'Content-Type': 'application/json',
    # 'Cookie': 'Hm_lvt_f32803886966beff8fa513f7a33dea1e=1726442768; __jsluid_s=d88cae93286614d37d244f1579777df3; SUNWAY-ESCM-COOKIE=88a7f2ec-63a6-483c-b19b-c79efb470f31; JSESSIONID=5C3AFC181C8A6D0DCC0547402F44140F',
    'Origin': 'https://ec.minmetals.com.cn',
    'Pragma': 'no-cache',
    'Referer': 'https://ec.minmetals.com.cn/open/home/purchase-info',
    'Sec-Fetch-Dest': 'empty',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Site': 'same-origin',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0',
    'sec-ch-ua': '"Chromium";v="128", "Not;A=Brand";v="24", "Microsoft Edge";v="128"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

# 请求获取公钥publicKey
publicRes = requests.post('https://ec.minmetals.com.cn/open/homepage/public', cookies=cookies, headers=headers)
publicKey = publicRes.text;
print(publicKey)

# python 通过PyExecJS 执行js
import execjs
js_code = open("webpack_case1.js", "r", encoding="utf-8").read()
# 使用  执行 JavaScript 代码
ctx = execjs.compile(js_code)
# 调用 JavaScript 函数，并计算结果
result = ctx.call("geainParam", publicKey, 2)
print('调用 JavaScript 函数的结果为:', result)


json_data = {
    # 'param': 'rGKPAl7Ka1CxqYHexMVr5Wvx2sYAhu1mUBGBvW6k2LMMzPGE91av7g7+Dlx1TJvENF3m6wFlRRKrs9YAdzPHggLk4e3IMXapakJMCK4+Ad1sI41AAwnQap76kj8gwmgNKS4zljrPu59bEqrn2OMbYhRO/GzWm2pD54vN2EFUVewxPX/eEmMMZ++Y8D0K8u2sN9JJVGYjBmRrmj/WA40x3zGUFDzkB4qyyNaER/X4gH9dB8AOgnA6VzH/3LFAakddV/I9+sPOrm/qyHQpiqRJnJn96Sw+qYtw+z5s44gSJn9I1oOKedKS4ktT1vkNqOa8JvF4bc2eeWAUKYQzCovEexRhPlEcAWNk2Mt6/1EHzYuN42hlDv7C8kNiplAfewqaexBSFIGMYDmOqK8uY4PdbNbPed/9lsboad4KoQVqGtdK9sr0/OWaPcmOpVYh1l5EGU2qURf4AVi26T/mtcKLpi+9VPtFstgWyPDiBcl9/DA22H6+z5lB+E7RvSamwlWPbAOdQ5ylEua3dso1cYw7PgH0DDOVxfMMNOwU7FaY40ap41paeMNLJ2VLWIumzGjSQEYgIGVHS0EkkGV5192FJKZ5L5TiboC5FasCcokkveu8TN3Fgi1I35dax4My/FxUNyPOUcGo8PKEEZCGNjdvrbAzteuKReAcnwOq5wTuh9w=',
    'param': result
}

response = requests.post(
    'https://ec.minmetals.com.cn/open/homepage/zbs/by-lx-page',
    cookies=cookies,
    headers=headers,
    json=json_data,
)

print(response.text)

# Note: json_data will not be serialized by requests
# exactly as it was in the original request.
#data = '{"param":"rGKPAl7Ka1CxqYHexMVr5Wvx2sYAhu1mUBGBvW6k2LMMzPGE91av7g7+Dlx1TJvENF3m6wFlRRKrs9YAdzPHggLk4e3IMXapakJMCK4+Ad1sI41AAwnQap76kj8gwmgNKS4zljrPu59bEqrn2OMbYhRO/GzWm2pD54vN2EFUVewxPX/eEmMMZ++Y8D0K8u2sN9JJVGYjBmRrmj/WA40x3zGUFDzkB4qyyNaER/X4gH9dB8AOgnA6VzH/3LFAakddV/I9+sPOrm/qyHQpiqRJnJn96Sw+qYtw+z5s44gSJn9I1oOKedKS4ktT1vkNqOa8JvF4bc2eeWAUKYQzCovEexRhPlEcAWNk2Mt6/1EHzYuN42hlDv7C8kNiplAfewqaexBSFIGMYDmOqK8uY4PdbNbPed/9lsboad4KoQVqGtdK9sr0/OWaPcmOpVYh1l5EGU2qURf4AVi26T/mtcKLpi+9VPtFstgWyPDiBcl9/DA22H6+z5lB+E7RvSamwlWPbAOdQ5ylEua3dso1cYw7PgH0DDOVxfMMNOwU7FaY40ap41paeMNLJ2VLWIumzGjSQEYgIGVHS0EkkGV5192FJKZ5L5TiboC5FasCcokkveu8TN3Fgi1I35dax4My/FxUNyPOUcGo8PKEEZCGNjdvrbAzteuKReAcnwOq5wTuh9w="}'
#response = requests.post('https://ec.minmetals.com.cn/open/homepage/zbs/by-lx-page', cookies=cookies, headers=headers, data=data)